//
//  SingleChooseViewController.m
//  DebugDemo
//
//  Created by XFB on 2019/1/17.
//  Copyright © 2019年 ZhouCheng. All rights reserved.
//  此思路好像不太好，好像经过多次的for循环遍历了

#import "SingleChooseViewController.h"
#import "ChooseModel.h"
#import "ChooseCell.h"

@interface SingleChooseViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *dataSource;

@end

@implementation SingleChooseViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setupBasicSet];
    [self setupUI];
}

- (void)setupBasicSet
{
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"单选1";
    
    UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"提交" style:UIBarButtonItemStyleDone target:self action:@selector(rightBarButtonItemDidClick:)];
    self.navigationItem.rightBarButtonItem = rightBarButtonItem;
}

- (void)rightBarButtonItemDidClick:(UIBarButtonItem *)item
{
    for (ChooseModel *model in self.dataSource) {
        if (model.isSelected) {
            NSLog(@"---%@---", model.theme);
            UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"标题" message:model.theme preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            }];
            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            }];
            [alertVC addAction:okAction];
            [alertVC addAction:cancelAction];
            [self.navigationController presentViewController:alertVC animated:YES completion:nil];
        } else {
            UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"标题" message:@"请选择一项" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            }];
            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            }];
            [alertVC addAction:okAction];
            [alertVC addAction:cancelAction];
            [self.navigationController presentViewController:alertVC animated:YES completion:nil];
        }
    }
}

- (void)setupUI
{
    [self.view addSubview:self.tableView];
    [self.tableView reloadData];
}

#pragma mark --- <UITableViewDataSource>
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ChooseCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ChooseCell"];
    cell.chooseModel = self.dataSource[indexPath.row];
    return cell;
}

#pragma mark --- <UITableViewDelegate>

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    for (ChooseModel *model in self.dataSource) {
        if ([model isEqual:self.dataSource[indexPath.row]]) {
            model.isSelected = YES;
        } else {
            model.isSelected = NO;
        }
    }
    [self.tableView reloadData];
}

- (UITableView *)tableView
{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, NavBarHeight, ScreenW, ScreenH-NavBarHeight-BottomHeight) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.tableFooterView = [UIView new];
        [_tableView registerNib:[UINib nibWithNibName:@"ChooseCell" bundle:nil] forCellReuseIdentifier:@"ChooseCell"];
    }
    return _tableView;
}

- (NSArray *)dataSource
{
    if (_dataSource == nil) {
        NSArray *plistArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"ChooseModel" ofType:@"plist"]];
        NSArray *modelArray = [ChooseModel mj_objectArrayWithKeyValuesArray:plistArray];
        
        NSMutableArray *mutableArray = @[].mutableCopy;
        for (ChooseModel *model in modelArray) {
            model.isSelected = NO;
            [mutableArray addObject:model];
        }
        _dataSource = [mutableArray mutableCopy];
    }
    return _dataSource;
}

@end
